package com.hdoit.ares.web.claim.wechat;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.claim.model.ClaimApplyfor;
import com.hdoit.ares.logical.claim.service.IClaimApplyforService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value="/wechat/claim/greenChannel/")
public class GreenChannelWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		return "areas/wechat/claim/greenChannel/";
	}
	
	private IClaimApplyforService claimApplyforService;

	public IClaimApplyforService getClaimApplyforService() {
		return claimApplyforService;
	}

	@Resource
	public void setClaimApplyforService(IClaimApplyforService claimApplyforService) {
		this.claimApplyforService = claimApplyforService;
	}
	
	private final String applyForClaimViewName = "applyforClaim";
	private final String submitApplicationViewName = "submitApplication";
	private final String myClaimViewName="myClaim";
	private final String claimListViewName = "claimList";
	private final String claimDetailViewName = "claimDetail";
	
	@RequestMapping(value = myClaimViewName, method = { RequestMethod.POST,RequestMethod.GET})
	public String myClaim(Model model) {
		return view(myClaimViewName);
	}
	
	
	@RequestMapping(value = claimListViewName,method = {RequestMethod.GET,RequestMethod.POST})
	public String claimList(Model model,String state){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);
		List<ClaimApplyfor> claimApplyfors = null;
		
		if("underway".equals(state)){
			claimApplyfors = claimApplyforService.queryClaim(customer.getId(), state);
			if(claimApplyfors.size()==0){
				model.addAttribute("msg", "您还没有进行中的理赔哦！");
			}else{
				model.addAttribute("msg", "");
			}
		}else if("finished".equals(state)){
			claimApplyfors = claimApplyforService.queryClaim(customer.getId(), state);
			if(claimApplyfors.size()==0){
				model.addAttribute("msg", "您还没有已完结的理赔哦！");
			}else{
				model.addAttribute("msg", "");
			}
		}

		model.addAttribute("claimApplyfors", claimApplyfors);
		return  view(claimListViewName);
	}
	
	@RequestMapping(value = applyForClaimViewName, method = { RequestMethod.GET, RequestMethod.POST})
	public String applyForClaim(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);
		return view(applyForClaimViewName);
	}
	
	@ResponseBody
	@RequestMapping(value = submitApplicationViewName, method = { RequestMethod.GET, RequestMethod.POST})
	public String submitApplication(Model model,ClaimApplyfor claimApplyfor) {
				WechatLoginModel loginModel = wechatSession.getCurrentLogin();
				model.addAttribute("loginModel", loginModel);
				Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
				model.addAttribute("customer", customer);
				
	            claimApplyfor.setId(CommonUtils.getKeyId());
	            claimApplyfor.setAid(CommonUtils.getKeyId());
	            claimApplyfor.setState("checking");
	            claimApplyfor.setCreateDate(DateUtils.getCurrent());
	            claimApplyfor.setDeleteMark(YesOrNo.no);
	            claimApplyfor.setModelStatus(ModelStatus.add);
	            claimApplyfor.setOrderNum("999");
	            claimApplyfor.setCustomerId(customer.getId());
	            
	            boolean result = claimApplyforService.applyChange(claimApplyfor);
	            return String.valueOf(result); 
	}
	
	
	@RequestMapping(value = claimDetailViewName, method = { RequestMethod.GET, RequestMethod.POST})
	public String claimDetail(Model model,String key) {
		ClaimApplyfor claimApplyfor = claimApplyforService.queryModel(key);
		model.addAttribute("claimApplyfor",claimApplyfor);
		return view(claimDetailViewName);
	}
	
}
